package telas;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dado.DAOUser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

import organizador.User;

public class ConsultaUser extends JFrame {

	private JPanel contentPane;
	private JTextField txtPesquisa;
	private JTable table;
	private JButton btnPesquisar;
	
	private ArrayList<User> users = new ArrayList<User>();
	private JButton btnNovoRegistro;
	private JButton btnApagar;
	private JButton btnAlterar;
	
	private DAOUser dadosUser = new DAOUser();
	private JComboBox cbbPesquisa;
	
	private User user = new User();
	private CadastraUser telaUser = new CadastraUser();
	private JButton btnCancelar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConsultaUser frame = new ConsultaUser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ConsultaUser() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 364);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblDigiteAlgumDado = new JLabel("Digite dados do cliente:");
		lblDigiteAlgumDado.setBounds(4, 24, 350, 14);
		contentPane.add(lblDigiteAlgumDado);
		
		txtPesquisa = new JTextField();
		txtPesquisa.setBounds(4, 49, 516, 20);
		contentPane.add(txtPesquisa);
		txtPesquisa.setColumns(10);
		
		btnPesquisar = new JButton("Pesquisar");
		btnPesquisar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				users = dadosUser.searchUser(txtPesquisa.getText());
				cbbPesquisa.removeAllItems();
				for (int i=0; i<users.size();i++){
					
					cbbPesquisa.addItem(users.get(i));
				}
			}
		});
		btnPesquisar.setBounds(4, 80, 91, 23);
		contentPane.add(btnPesquisar);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null},
			},
			new String[] {
				"idUser", "Nome", "CPF", "Telefone", "E-mail", "idLivro", "Livro ", "Devolu\u00E7\u00E3o"
			}
		) {
			Class[] columnTypes = new Class[] {
				Integer.class, String.class, String.class, String.class, String.class, Integer.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.getColumnModel().getColumn(1).setPreferredWidth(174);
		table.getColumnModel().getColumn(4).setPreferredWidth(146);
		table.setBounds(4, 131, 516, 157);
		contentPane.add(table);
		
		btnNovoRegistro = new JButton("Novo Registro");
		btnNovoRegistro.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				telaUser.abreTela(1, null);
				telaUser.setVisible(true);				
			}
		});
		btnNovoRegistro.setBounds(14, 299, 119, 23);
		contentPane.add(btnNovoRegistro);
		
		btnAlterar = new JButton("Alterar");
		btnAlterar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				user =(User) cbbPesquisa.getSelectedItem();
				telaUser.abreTela(2, user);
				telaUser.setVisible(true);	
				
				
			}
		});
		btnAlterar.setBounds(143, 299, 116, 23);
		contentPane.add(btnAlterar);
		
		btnApagar = new JButton("Apagar");
		btnApagar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				user =(User) cbbPesquisa.getSelectedItem();
				
				dadosUser.deleteUser(user);
			}
		});
		btnApagar.setBounds(269, 299, 126, 23);
		contentPane.add(btnApagar);
		
		cbbPesquisa = new JComboBox();
		cbbPesquisa.setBounds(104, 80, 416, 22);
		contentPane.add(cbbPesquisa);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtPesquisa.setText(null);
				cbbPesquisa.removeAll();
				dispose();
			}
		});
		btnCancelar.setBounds(405, 299, 103, 23);
		contentPane.add(btnCancelar);
	}
}
